var info = require('../infos/info')
/**
 * 1.基础模块
 */
// 获取用户列表
exports.userList = function(req,res) {
  var page = req.query['page']
  var limit = req.query['limit']
  var getStart = (page - 1) * limit
  var sql = 'select * from user limit ' + getStart + ',' + limit
  var sqlen = 'select count(*) as total from user'
  var promise = new Promise(function(resolve,reject){
    info.getInfoCount(sqlen,[],function(len){
      resolve(len)
    })
  })
  promise.then(function(len){
    info.findAllInfo(sql,[],function(results){
      results.count = len
      res.send(JSON.stringify(results))
    })
  })
}
// 获取某个用户数据
exports.userSearch = function(req,res){
  var num = req.query['num']
  var sql = `select * from user where num like '%${num}%'`
  info.findOneInfo(sql,[num],function(results){
    res.send(JSON.stringify(results))
  })
}
// 用户密码更改
exports.userUpdate = function(req,res){
  var num = req.query['num']
  var password = req.query['password']
  var sql = 'update user set password=? where num=?'
  var params = [password,num]
  if (num == req.session.info['num']) {
    return res.send({affectedRows:0})
  }
  info.updateInfo(sql,params,function(results){
    res.send(results)
  })
}
// 设置权限
exports.userTypeSet = function(req,res){
  var num = req.query['num']
  var type = req.query['type']
  var sql = 'update user set type=? where num=?'
  var params = [type,num]
  if (num == req.session.info['num']) {
    return res.send({affectedRows:0})
  }
  info.updateInfo(sql,params,function(results){
    res.send(results)
  })
}
// 用户数据删除
exports.userDelete = function(req,res){
  var nums = req.query['num']
  var temp = nums.split(',')
  for(var index in temp){
    if (temp[index] == req.session.info['num']) {
      return res.send({affectedRows:0})
    }
    temp[index] = "'"+temp[index]+"'"
  }
  var sql = 'delete from user where num in('+temp.toString()+')'
  info.deleteInfo(sql,[],function(results){
    res.send(results)
  })
}

// 获取用户信息列表
exports.userInfoList = function(req,res){
  var page = req.query['page']
  var limit = req.query['limit']
  var getStart = (page - 1) * limit
  var sql = 'select * from userinfo limit ' + getStart + ',' + limit
  var sqlen = 'select count(*) as total from userinfo'
  var promise = new Promise(function(resolve,reject){
    info.getInfoCount(sqlen,[],function(len){
      resolve(len)
    })
  })
  promise.then(function(len){
    info.findAllInfo(sql,[],function(results){
      results.count = len
      res.send(JSON.stringify(results))
    })
  })
}
// 查找用户信息
exports.userInfoSearch = function(req,res){
  var num = req.query['num']
  var sql = 'select * from userinfo where num=?'
  info.findOneInfo(sql,[num],function(results){
    res.send(JSON.stringify(results))
  })
}
// 批量/单个删除用户信息
exports.userInfoDelete = function(req,res){
  var nums = req.query['num']
  var temp = nums.split(',')
  for(var index in temp){
    if (temp[index] == req.session.info['num']) {
      return res.send({affectedRows:0})
    }
    temp[index] = "'"+temp[index]+"'"
  }
  var sql = 'delete from userinfo where num in('+temp.toString()+')'
  info.deleteInfo(sql,[],function(results){
    res.send(results)
  })
}

/**
 * 2.课程模块
 */
exports.courseInfoList = function(req,res){
  var page = req.query['page']
  var limit = req.query['limit']
  var getStart = (page - 1) * limit
  var sql = 'select * from courseinfo limit ' + getStart + ',' + limit
  var sqlen = 'select count(*) as total from courseinfo'
  var promise = new Promise(function(resolve,reject){
    info.getInfoCount(sqlen,[],function(len){
      resolve(len)
    })
  })
  promise.then(function(len){
    info.findAllInfo(sql,[],function(results){
      results.count = len
      res.send(JSON.stringify(results))
    })
  })
}
exports.courseInfoDelete = function(req,res){
  var cnums = req.query['cnum']
  var temp = cnums.split(',')
  for(var index in temp){
    temp[index] = "'"+temp[index]+"'"
  }
  var sql = 'delete from courseinfo where cnum in('+temp.toString()+')'
  info.deleteInfo(sql,[],function(results){
    res.send(results)
  })
}
exports.courseInfoSearch = function(req,res){
  var cnum = req.query['cnum']
  var sql = 'select * from courseinfo where cnum=?'
  info.findOneInfo(sql,[cnum],function(results){
    res.send(JSON.stringify(results))
  })
}

exports.scheduleInfoList = function(req,res){
  // var tname = req.query['tname']
  var page = req.query['page']
  var limit = req.query['limit']
  var getStart = (page - 1) * limit
  var sql = 'select * from scheduleinfo limit ' + getStart + ',' + limit
  // var sql = `select * from scheduleinfo where tname='${tname}' limit ${getStart},${limit}`
  var sqlen = 'select count(*) as total from scheduleinfo'
  var promise = new Promise(function(resolve,reject){
    info.getInfoCount(sqlen,[],function(len){
      resolve(len)
    })
  })
  promise.then(function(len){
    info.findAllInfo(sql,[],function(results){
      results.count = len
      res.send(JSON.stringify(results))
    })
  })
}
exports.scheduleInfoSearch = function(req,res){
  var tname = req.query['tname']
  var sql = `select * from scheduleinfo where tname like '%${tname}%'`
  info.findOneInfo(sql,[],function(results){
    res.send(JSON.stringify(results))
  })
}
exports.scheduleInfoDelete = function(req,res){
  var sids = req.query['sid']
  var sql = 'delete from scheduleinfo where sid in('+sids+')'
  info.deleteInfo(sql,[],function(results){
    res.send(results)
  })
}
/**
 * 3.机房模块
 */
exports.machineInfoList = function(req,res){
  var page = req.query['page']
  var limit = req.query['limit']
  var getStart = (page - 1) * limit
  var sql = 'select * from roominfo limit ' + getStart + ',' + limit
  var sqlen = 'select count(*) as total from roominfo'
  var promise = new Promise(function(resolve,reject){
    info.getInfoCount(sqlen,[],function(len){
      resolve(len)
    })
  })
  promise.then(function(len){
    info.findAllInfo(sql,[],function(results){
      results.count = len
      res.send(JSON.stringify(results))
    })
  })
}
exports.machineInfoDelete = function(req,res){
  var rnums = req.query['rnum']
  var sql = 'delete from roominfo where rid in('+rnums+')'
  info.deleteInfo(sql,[],function(results){
    res.send(results)
  })
}
exports.machineInfoSearch = function(req,res){
  var rnum = req.query['rnum']
  var sql = `select * from roominfo where rnum like '%${rnum}%'`
  info.findOneInfo(sql,[],function(results){
    res.send(JSON.stringify(results))
  })
}

exports.bookInfoList = function(req,res){

}
exports.bookInfoDelete = function(req,res){
  
}
exports.bookInfoSearch = function(req,res){
  
}

//教师模块
exports.getPersionInfo = function(req,res){
  var obj = req.session.info
  var sql = 'select * from userinfo where num=?'
  info.findOneInfo(sql,[obj.num],function(results){
    res.send(JSON.stringify(results))
  })
}
exports.persionScheduleInfoList = function(req,res){
  var tname = req.session.info['name']
  var page = req.query['page']
  var limit = req.query['limit']
  var getStart = (page - 1) * limit
  var sql = `select * from scheduleinfo where tname='${tname}' limit ${getStart},${limit}`
  var sqlen = 'select count(*) as total from scheduleinfo'
  var promise = new Promise(function(resolve,reject){
    info.getInfoCount(sqlen,[],function(len){
      resolve(len)
    })
  })
  promise.then(function(len){
    info.findAllInfo(sql,[],function(results){
      results.count = len
      res.send(JSON.stringify(results))
    })
  })
}